 <?php

	/**
	 * 
	 * 图片处理类
	 * @author tangfengqiao
	 *
	 */
	class Fengqiaoju_Tool_Image {

		/**
		 * 生成等比缩略图
		 */
		public static function thumb($srcFile , $toW , $toH , $toFile = "") {

			if ($toFile == "") {
				$exts = @split ( "[/\\.]", $srcFile );
				$n = count ( $exts ) - 1;
				$extName = $exts [$n]; //扩展名
				$_path = explode ( "." . $extName, $srcFile );
				$_host = parse_url($_path[0]); //获取路径,去除域名信息
				$toFile = ".".$_host['path'] . '_thumb.' . $extName;
			}
			$info = "";
			$data = GetImageSize ( $srcFile, $info );
			switch ($data [2]) {
				case 1 :
					if (! function_exists ( "imagecreatefromgif" )) {
						echo "你的GD库不能使用GIF格式的图片，请使用Jpeg或PNG格式！";
						exit ();
					}
					$im = ImageCreateFromGIF ( $srcFile );
					break;
				case 2 :
					if (! function_exists ( "imagecreatefromjpeg" )) {
						echo "你的GD库不能使用jpeg格式的图片，请使用其它格式的图片！";
						exit ();
					}
					$im = ImageCreateFromJpeg ( $srcFile );
					break;
				case 3 :
					$im = ImageCreateFromPNG ( $srcFile );
					break;
			}
			$srcW = ImageSX ( $im );
			$srcH = ImageSY ( $im );
			$toWH = $toW / $toH;
			$srcWH = $srcW / $srcH;
			if ($toWH <= $srcWH) {
				$ftoW = $toW;
				$ftoH = $ftoW * ($srcH / $srcW);
			}
			else {
				$ftoH = $toH;
				$ftoW = $ftoH * ($srcW / $srcH);
			}
			if ($srcW > $toW || $srcH > $toH) {
				if (function_exists ( "imagecreatetruecolor" )) {
					@$ni = ImageCreateTrueColor ( $ftoW, $ftoH );
					if ($ni) ImageCopyResampled ( $ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH );
					else {
						$ni = ImageCreate ( $ftoW, $ftoH );
						ImageCopyResized ( $ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH );
					}
				}
				else {
					$ni = ImageCreate ( $ftoW, $ftoH );
					ImageCopyResized ( $ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH );
				}
				if (function_exists ( 'imagejpeg' )) ImageJpeg ( $ni, $toFile );
				else ImagePNG ( $ni, $toFile );
				ImageDestroy ( $ni );
			}
			ImageDestroy ( $im );
			return ltrim($toFile,'.');
		}
	}
	?>